package com.simpns.io;

import com.simpns.io.entity.DevReturn;
import com.sun.jna.Pointer;

/**
 * @Description TODO
 * @Author simpns
 * @Date 2024/12/30 10:33
 */
public class Line7IoDevice {
    public static int isSerialError = 0;
    private static Pointer pointer;
    static DevReturn[] status = (DevReturn[]) new DevReturn().toArray(8);

    private static DevReturn[] getDevReturns() {
        return status;
    }

    private static void getIssueResult(DevReturn[] status, int result) {
        int iLogicCode = status[0].iLogicCode;
        int iPhyCode = status[0].iPhyCode;
        int iHandle = status[0].iHandle;
        if (result != 0 || iPhyCode != 0 || iLogicCode != 0) {
            isSerialError = 1;
        } else {
            isSerialError = 0;
        }
    }

    public static IORet setCommPra() {
        DevReturn[] devReturns = getDevReturns();
        pointer = IOExtendDev.INSTANCE.GRGIO_hOpenLogicDevice("IOEXTENDDEV", getDevReturns());
        int i = IOExtendDev.INSTANCE.GRGIO_iSetCommPara(pointer, devReturns);
        return new IORet(i, devReturns[0]);
    }

    //初始化
    public static IORet init() {
        DevReturn[] devReturns = getDevReturns();
        int i = IOExtendDev.INSTANCE.GRGIO_iInit(pointer, devReturns);
        return new IORet(i, devReturns[0]);
    }

    //获取灯状态
    public static IORet getLightStatus(int lightNum) {
        DevReturn[] devReturns = getDevReturns();
        int i = IOExtendDev.INSTANCE.GRGIO_iGetLightStatus(pointer, lightNum, devReturns);
        return new IORet(i, devReturns[0]);
    }

    /**
     * @description: 设置灯状态 com2-进站io com4-出站io
     *     lightNum:
     *     编号:0-全部
     *     1-
     *     2-
     *     3-
     *     编号:4-回收口灯
     *     编号:5-顶灯 蓝
     *     编号:6-顶灯 红
     *     编号:7-顶灯 绿
     *     8-
     *     编号:9-方向灯 绿
     *     编号:10-方向灯 红
     *     编号:11-方向灯 蓝
     *     编号:12-方向灯 特殊灯
     * @author: simpns
     * @date: 2025/5/16 10:18
     * @param lightNum 灯编号
     * @param status 0-关,1-开,2,3,4-闪烁加强
     * @return IORet
     */
    public static IORet setLightStatus(int lightNum, int status) {
        DevReturn[] devReturns = getDevReturns();
        int i = IOExtendDev.INSTANCE.GRGIO_iSetLightStatus(pointer, (byte) 0, lightNum, status, devReturns);
        return new IORet(i, devReturns[0]);
    }

    /**
     * @description: 目前不可用
     * @author: simpns
     * @date: 2025/5/16 10:18
     * @param lightNums
     * @param status
     * @return IORet
     */
    public static IORet setLightStatusEx(int[] lightNums, int[] status) {
        DevReturn[] devReturns = getDevReturns();
        int length = lightNums.length;
        byte[] pbData = new byte[length];
        for (byte i = 0; i < length; i++) {
            pbData[i] = 0;
        }
        int i = IOExtendDev.INSTANCE.GRGIO_iSetLightStatusEx(pointer, pbData, lightNums, status, length, devReturns);
        return new IORet(i, devReturns[0]);
    }

    /**
     * @description: 获取到位状态
     * com2-进站io
     * 编号:1-出站边门
     * 编号:2-进站边门
     * 编号:3-进站上盖
     * com4-出站io
     * 编号:1- 出站边门
     * 编号:2- 进站边门
     * 编号:3- 出站上盖
     *
     * @author: simpns
     * @date: 2025/5/16 10:17
     * @param doorNum 到位编号
     * @return IORet
     */
    public static IORet getDoorStatus(int doorNum) {
        DevReturn[] devReturns = getDevReturns();
        int i = IOExtendDev.INSTANCE.GRGIO_iGetDoorStatus(pointer, doorNum, devReturns);
        return new IORet(i, devReturns[0]);
    }

    //获取版本号
    public static String getVersion() {
        DevReturn[] devReturns = getDevReturns();
        String version = null;
        int i = IOExtendDev.INSTANCE.GRGIO_iGetDevVersion(pointer, version);
        return version;
    }

    //获取io板状态
    public static IORet getIoBoardStatus() {
        DevReturn[] devReturns = getDevReturns();
        int i = IOExtendDev.INSTANCE.GRGIO_iGetStatus(pointer, devReturns);
        return new IORet(i, devReturns[0]);
    }
}
